package lsem.eclipse.vdf.ui;

import java.net.URI;

import lsem.eclipse.vdf.ui.natures.VDFProjectSupport;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
import org.guvnor.tools.wizards.GuvWizardModel;
import org.guvnor.tools.wizards.GuvnorMainConfigPage;
import org.guvnor.tools.wizards.IGuvnorWizard;
import org.guvnor.tools.wizards.WizardUtils;
import org.guvnor.tools.Messages;
import org.guvnor.tools.Activator;

public class NewWizard extends Wizard implements INewWizard, IGuvnorWizard,
		IExecutableExtension {

	private IConfigurationElement _configurationElement;

	private WizardNewProjectCreationPage _pageOne;
	private static final String WIZARD_NAME = "Vertical Domain Facilities"; //$NON-NLS-1$
	private static final String PAGE_NAME = "Vertical Domain Facilities Project Wizard"; //$NON-NLS-1$

	private GuvnorMainConfigPage mainPage;

	private GuvWizardModel model = new GuvWizardModel();

	public NewWizard() {
		setWindowTitle(WIZARD_NAME);
	}

	public GuvWizardModel getModel() {
		return model;
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean performFinish() {
		String name = _pageOne.getProjectName();
		URI location = null;
		if (!_pageOne.useDefaults()) {
			location = _pageOne.getLocationURI();
		} // else location == null

		VDFProjectSupport.createProject(name, location);

		BasicNewProjectResourceWizard.updatePerspective(_configurationElement);

		try {
			WizardUtils.createGuvnorRepository(model);
		} catch (Exception e) {
			Activator.getDefault().displayError(IStatus.ERROR, e.getMessage(),
					e, true);
		}

		return true;
	}

	@Override
	public void addPages() {
		super.addPages();

		_pageOne = new WizardNewProjectCreationPage(PAGE_NAME);
		_pageOne.setTitle("Vertical Domain Facilities");
		_pageOne.setDescription("Create something from scratch.");

		addPage(_pageOne);

		mainPage = new GuvnorMainConfigPage(
				"config_page", Messages.getString("new.guvnor.loc"), //$NON-NLS-1$ //$NON-NLS-2$
				Activator.getImageDescriptor(Activator.IMG_GUVLOCADD));
		mainPage.setDescription(Messages.getString("new.guvnor.loc.desc")); //$NON-NLS-1$
		addPage(mainPage);

	}

	@Override
	public boolean canFinish() {
		return mainPage.isPageComplete();
	}

	@Override
	public void setInitializationData(IConfigurationElement config,
			String propertyName, Object data) throws CoreException {
		_configurationElement = config;
	}

}
